<?php

/*
<channel>
  <link>http://maps.google.com</link>
  <title>�ش��ͧ�ͤ������������</title>
  <description><![CDATA[xxxx]]></description>
  <item>
    <guid isPermaLink="false">000493180bec6f93e23ea</guid>
    <pubDate>Thu, 21 Oct 2010 03:23:15 +0000</pubDate>

    <title>zzzzz</title>
    <description><![CDATA[<div dir="ltr">2010-10-20 18.43  yyyyy </div>]]></description>
    <author></author>
    <georss:point>
      15.066819 100.848999
    </georss:point>
    <georss:elev>0.000000</georss:elev>
  </item>
</channel>
*/
class ReaderXML
{
  private $xml;
  private $icon;
  private $reference;

  function ReaderXML ($xml, $icon, $reference)
  {
    $this->xml = $xml;
    $this->icon = $icon;
    $this->reference = $reference;
  }

  public function getReadXML ()
  {
    $xmlDoc = new DOMDocument('1.0', 'UTF-8');
    $xmlDoc->load($this->xml);
    //get elements from "<channel>"
    $channel = $xmlDoc->getElementsByTagName('channel')->item(0);
    $channel_title = $channel->getElementsByTagName('title')->item(0)->childNodes->item(
    0)->nodeValue;
    $channel_link = $channel->getElementsByTagName('link')->item(0)->childNodes->item(
    0)->nodeValue;
    $channel_desc = $channel->getElementsByTagName('description')->item(
    0)->childNodes->item(0)->nodeValue;
    $x = $xmlDoc->getElementsByTagName('item');
    $i = 0;
    foreach ($xmlDoc->getElementsByTagNameNS(
    'http://www.georss.org/georss', 'point') as $element)
    {
      $item_title = $x->item($i)
        ->getElementsByTagName('title')
        ->item(0)->childNodes->item(0)->nodeValue;
      //var_dump($item_title);
      $item_desc = $this->html2txt(
      $x->item($i)
        ->getElementsByTagName('description')
        ->item(0)->childNodes->item(0)->nodeValue);
      //$item_aut=$x->item($i)->getElementsByTagName('author')->item(0)->childNodes->item(0)->nodeValue;
      $item_date = $x->item($i)
        ->getElementsByTagName('pubDate')
        ->item(0)->childNodes->item(0)->nodeValue;
      $item_lat_lon = $element->nodeValue;
      $expladLatLon = explode(" ", trim($item_lat_lon));
      $lat = $expladLatLon[0];
      $lon = $expladLatLon[1];
      $result['layer'][$i]['title'] = $item_title;
      $result['layer'][$i]['description'] = $this->reference;
      $result['layer'][$i]['icon'] = $this->icon;
      $result['layer'][$i]['detail'] = $item_desc;
      $result['layer'][$i]['latitude'] = $lat;
      $result['layer'][$i]['longitude'] = $lon;
      $i ++;
    }
    return $result;
  }

  public function getReadUshahidiXML ()
  {
    $xmlDoc = new DOMDocument('1.0', 'UTF-8');
    $xmlDoc->load($this->xml);
    //get elements from "<channel>"
    $channel = $xmlDoc->getElementsByTagName('channel')->item(0);
    $channel_title = $channel->getElementsByTagName('title')->item(0)->childNodes->item(
    0)->nodeValue;
    $channel_link = $channel->getElementsByTagName('link')->item(0)->childNodes->item(
    0)->nodeValue;
    $channel_desc = $channel->getElementsByTagName('description')->item(
    0)->childNodes->item(0)->nodeValue;
    $x = $xmlDoc->getElementsByTagName('item');
    $i = 0;
    foreach ($xmlDoc->getElementsByTagNameNS(
    'http://www.georss.org/georss', 'point') as $element)
    {
      $item_title = $x->item($i)
        ->getElementsByTagName('title')
        ->item(0)->childNodes->item(0)->nodeValue;
      //var_dump($item_title);
      $item_desc = $this->html2txt(
      $x->item($i)
        ->getElementsByTagName('description')
        ->item(0)->childNodes->item(0)->nodeValue);
      //$item_aut=$x->item($i)->getElementsByTagName('author')->item(0)->childNodes->item(0)->nodeValue;
      $item_date = $x->item($i)
        ->getElementsByTagName('pubDate')
        ->item(0)->childNodes->item(0)->nodeValue;
      $item_lat_lon = $element->nodeValue;
      $expladLatLon = explode(" ", trim($item_lat_lon));
      $lat = $expladLatLon[0];
      $lon = $expladLatLon[1];
      $result['layer'][$i]['title'] = $item_title;
      $result['layer'][$i]['description'] = $this->reference;
      $result['layer'][$i]['icon'] = $this->icon;
      $result['layer'][$i]['detail'] = $item_desc;
      //$result['layer'][$i]['detail'] = $this->reference;
      $result['layer'][$i]['latitude'] = $lat;
      $result['layer'][$i]['longitude'] = $lon;
      $i ++;
    }
    return $result;
  }

  private function html2txt ($document)
  {
    $search = array(
      '@<script[^>]*?>.*?</script>@si', // Strip out javascript 
'@<[\/\!]*?[^<>]*?>@si',  // Strip out HTML tags 
    '@<style[^>]*?>.*?</style>@siU', // Strip style tags properly 
'@<![\s\S]*?--[ \t\n\r]*>@' // Strip multi-line comments including CDATA 
    );
    $text = preg_replace($search, '', $document);
    return $text;
  }
}
?>